#coding=utf-8
'''
    时间、日期转换
    import datatime
    更复杂的日期操作 可以使用dateutil模块
'''
# 计算时间间隔
from datetime import timedelta
a = timedelta(days=2, hours=6)
b = timedelta(hours=4.5)
c = a + b
print(c)
print(c.days)
print(c.seconds)
print(c.seconds / 3600)
print(c.total_seconds() / 3600)

# 表示具体的日期和时间
from datetime import datetime
a = datetime(2012, 9, 23)
print(a + timedelta(days=10))
b = datetime(2012, 12, 21)
d = b - a
print(d.days)

now = datetime.today()
print(now)
print(now + timedelta(minutes=10))

# 当进行日期计算时，注意：datetime会自动处理闰年
a = datetime(2012, 3, 1)
b = datetime(2012, 2, 28)
print(a - b)
print((a - b).days)

c = datetime(2013, 3, 1)
d = datetime(2013, 2, 28)
print((c - d).days)

## dateutil模块
a = datetime(2012, 9, 23)
# print(a + timedelta(months=2))  # 会报错
from dateutil.relativedelta import  relativedelta
print(a + relativedelta(months=+1))
print(a + relativedelta(months=+4))

# time between two dates
b = datetime(2012, 12, 21)
d = b - a
print(d)
print(d.months)
print(d.days)